/**
 * 
 */
package org.t2.backstage.module.player;

import org.banish.sql.core.util.QuerySet;
import org.gaming.backstage.PageData;
import org.gaming.backstage.interceptor.RoleContext;
import org.gaming.backstage.module.user.service.UserService;
import org.gaming.service.apidoc.annotation.ApiDocument;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.t2.backstage.module.gameserver.model.GameChannel;
import org.t2.backstage.module.gameserver.service.GameChannelService;
import org.t2.backstage.module.player.model.WhiteList;
import org.t2.backstage.module.player.query.WhiteListQuery;
import org.t2.backstage.module.player.service.WhiteListService;

import sh.t2.share.apiutil.GameApi;

/**
 * @author YY
 *
 */
@Controller
public class WhiteListController {
	
	@Autowired
	private WhiteListService whiteListService;
	@Autowired
	private UserService userService;
	@Autowired
	private GameChannelService gameChannelService;
	
	@ApiDocument("请求白名单数据")
	@RequestMapping(value = "/whiteList/data.auth")
	@ResponseBody
	public PageData<WhiteList> data(WhiteListQuery query) {
		QuerySet querySet = new QuerySet();
		if(query.getOpenId() != null && !"".equals(query.getOpenId())) {
			querySet.addCondition("open_id like ?", "%" + query.getOpenId() + "%");
		}
		querySet.orderBy("order by id desc");
		querySet.limit(query.getPage(), query.getLimit());
		querySet.formWhere();
		
		return whiteListService.query(querySet);
	}
	
	@ApiDocument("添加白名单")
	@RequestMapping(value = "/whiteList/add.authw")
	@ResponseBody
	public WhiteList save(@RequestParam("channelId") long channelId, @RequestParam("openId") String openId,
			@RequestParam("remark") String remark) {
		RoleContext roleContext = userService.getCurrUser();
		GameChannel gameChannel = gameChannelService.getEntity(0, channelId);
		
		WhiteList whiteList = new WhiteList();
		whiteList.setChannelId(gameChannel.getId());
		whiteList.setChannelName(gameChannel.getName());
		whiteList.setOpenId(openId);
		whiteList.setUserId(roleContext.getId());
		whiteList.setUserName(roleContext.getName());
		whiteList.setRemark(remark);
		whiteListService.insert(whiteList);
		
		return whiteList;
	}
	
	@ApiDocument("删除白名单")
	@RequestMapping(value = "/whiteList/delete.authw")
	@ResponseBody
	public void delete(@RequestParam("id") long id) {
		WhiteList whiteList = whiteListService.getEntity(id);
		whiteListService.delete(whiteList);
	}
	
	@ApiDocument("是否为白名单，给游戏服的接口")
	@RequestMapping(value = GameApi.isWhiteList)
	@ResponseBody
	public String isWhiteList(@RequestParam("channelId") long channelId, @RequestParam("openId") String openId) {
		QuerySet querySet = new QuerySet();
		querySet.addCondition("channel_id = ?", channelId);
		querySet.addCondition("open_id = ?", openId);
		WhiteList whiteList = whiteListService.queryOne(querySet);
		
		if(whiteList != null) {
			return "true";
		} else {
			return "false";
		}
	}
}
